package com.jhws.sys.cms.service;

import com.jhws.common.base.bo.Global;
import com.jhws.core.db.BaseDao;
import com.jhws.core.exception.BizException;
import com.jhws.sys.cms.bo.NodePrivateInfo;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.sys.user.bo.UserCommsBo;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.sys.user.service.UserCommsService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SuppressWarnings("Duplicates")
@Service
public class NodePrivateInfoService {
    @Resource
    private BaseDao BaseDao;
    @Resource
    private ITblUserDimService userDimService;
    @Resource
    private UserCommsService userCommsService;
    @Resource
    private CmsRedisService redisService;

    /**
     * 保存节点别名，如果有id，则更新
     *
     * @param nodePrivateInfo
     */
    public void save(NodePrivateInfo nodePrivateInfo) {
        if (StringUtils.isEmpty(nodePrivateInfo.getUser()) ||
                StringUtils.isEmpty(nodePrivateInfo.getTypeCode())) {
            throw new BizException("参数错误！");
        }
        List<NodePrivateInfo> nodePrivateInfos = BaseDao.findList("from NodePrivateInfo where " +
                "user='" + nodePrivateInfo.getUser() + "' and type_code='" + nodePrivateInfo.getTypeCode() + "'");
        if (nodePrivateInfos != null && nodePrivateInfos.size() > 0) {
            NodePrivateInfo oldNodeAliases = nodePrivateInfos.get(0);
            nodePrivateInfo.setId(oldNodeAliases.getId());
            nodePrivateInfo.setCreateTime(oldNodeAliases.getCreateTime());
            nodePrivateInfo = BaseDao.h().merge(nodePrivateInfo);
            BaseDao.h().update(nodePrivateInfo);
        } else {
            nodePrivateInfo.setCreateTime(new Date());
            BaseDao.h().save(nodePrivateInfo);
        }
        //清空一下redis
        redisService.emptyRedisCMS();
    }

    /**
     * 条件查询类型
     *
     * @return
     */
    public NodePrivateInfo queryNodePrivateInfo(Map map) {
        String commCode = MapUtils.getString(map, "commCode");
        String logName = "";
        if (StringUtils.isEmpty(commCode)) {
            TblUserDimBo userDimBo = userDimService.currentUser();
            logName = userDimBo.getLogName();
        } else if (commCode.equals(Global.DEFAULT_COMM)) {
            List<UserCommsBo> userCommsBoList = userCommsService.findUserCommsByWhere(" where comm_code = '" + commCode + "' and role_name='" + Global.ROLE_SUPPER_ADMIN_CMS + "'");
            if (userCommsBoList != null && userCommsBoList.size() > 0) {
                logName = userCommsBoList.get(0).getUserName();
            }
        } else {
            List<UserCommsBo> userCommsBoList = userCommsService.findUserCommsByWhere(" where comm_code = '" + commCode + "' and role_name='" + Global.ROLE_SEC_LEVEL_ADMIN + "'");
            if (userCommsBoList != null && userCommsBoList.size() > 0) {
                logName = userCommsBoList.get(0).getUserName();
            }
        }
        String typeCode = MapUtils.getString(map, "typeCode");
        List<NodePrivateInfo> nodePrivateInfos = BaseDao.findList("from NodePrivateInfo where " +
                "user='" + logName + "' and type_code='" + typeCode + "'");
        if (nodePrivateInfos != null && nodePrivateInfos.size() > 0)
            return nodePrivateInfos.get(0);
        else
            return null;
    }

    /**
     * 条件删除资源
     *
     * @param where 条件
     */
    public void deleteAliases(String where) {
        String sql = "delete from cms_node_private_info " + where;
        BaseDao.j().execute(sql);
        //清空一下redis
        redisService.emptyRedisCMS();
    }
}
